// TreeModel.h
#ifndef TREEMODEL_H
#define TREEMODEL_H

#include <QAbstractItemModel>
#include <QVector>
#include <QVariant>

class TreeItem {
public:
    explicit TreeItem(const QVariant &data, TreeItem *parent = nullptr);
    ~TreeItem();

    TreeItem *child(int number);
    int childCount() const;
    int columnCount() const;
    QVariant data(int column) const;
    bool insertChildren(int position, int count, int columns);
    bool insertColumns(int position, int count);
    TreeItem *parent();
    bool removeChildren(int position, int count);
    bool removeColumns(int position, int count);
    int row() const;
    bool setData(int column, const QVariant &value);

private:
    QList<TreeItem*> childItems;
    QVariant itemData;
    TreeItem *parentItem;
};

class TreeModel : public QAbstractItemModel {
    Q_OBJECT

public:
    explicit TreeModel(const QStringList &headers, QObject *parent = nullptr);
    ~TreeModel();

    QVariant data(const QModelIndex &index, int role) const override;
    Qt::ItemFlags flags(const QModelIndex &index) const override;
    QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
    QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
    QModelIndex parent(const QModelIndex &index) const override;
    int rowCount(const QModelIndex &parent = QModelIndex()) const override;
    int columnCount(const QModelIndex &parent = QModelIndex()) const override;

    bool insertColumns(int position, int columns, const QModelIndex &parent = QModelIndex()) override;
    bool insertRows(int position, int rows, const QModelIndex &parent = QModelIndex()) override;
    bool removeColumns(int position, int columns, const QModelIndex &parent = QModelIndex()) override;
    bool removeRows(int position, int rows, const QModelIndex &parent = QModelIndex()) override;

    Q_INVOKABLE void setupModelData(const QStringList &lines, TreeItem *parent);

private:
    TreeItem *rootItem;
};

#endif // TREEMODEL_H
